package edu.guet.server.controller;

import edu.guet.server.model.Question;
import edu.guet.server.model.dto.*;
import edu.guet.server.service.PaperService;
import edu.guet.server.service.UnitTestService;
import edu.guet.server.utils.AliOSSUtils;
import edu.guet.server.utils.CompareCurTime;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import edu.guet.server.service.UnitService;
import edu.guet.server.utils.ResultAPI;
import edu.guet.server.model.Unit;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/unit")
public class UnitController {
    @Autowired
    UnitService unitService;
    @Autowired
    private AliOSSUtils aliOSSUtils;
    @Autowired
    private UnitTestService unitTestService;
    @Autowired
    private PaperService paperService;


    @RequestMapping("/setUnit")
    public ResultAPI setUnit(@RequestBody Unit unit){
        return unitService.setUnit(unit);
    }
    
    @RequestMapping("/resetUnit")
    public ResultAPI resetUnit(@RequestBody Unit date){
        return unitService.resetUnit(date);
    }


    @PostMapping("/setUnit/addResource")
    public ResultAPI addUnitVideo(@RequestBody UnitDTO unitDTO){
        //ppt和视频都在这里
        //添加资源和单元资源数量都在这里了
        log.info("添加单元资料！！{}",unitDTO.toString());
        unitService.addUnitResource(unitDTO.getUnit_id(),unitDTO.getResource_id());
        return ResultAPI.success();
    }

    /**
     * 获取小节下的视频和资料
     * @return
     */
    @GetMapping("/showUnitResouList")
    public ResultAPI showUnitResource(@RequestParam Integer unit_id, @RequestParam String resource_type){
        log.info("showUnitResouList啊啊啊啊"+unit_id+resource_type);
        List<UnitResourceDTO> showlist=unitService.showUnitResouList(unit_id,resource_type);
        return ResultAPI.success(showlist);
    }
    @DeleteMapping("/deleteUnitResource")
    public ResultAPI deleteUnitResource(@RequestBody UnitResourceDTO unitResourceDTO){
        unitService.deleteUnitResource(unitResourceDTO);
        return ResultAPI.success();
    }
    @DeleteMapping("/deleteUnit/{unit_id}")
    public ResultAPI deleteUnit(@PathVariable Integer unit_id){
        unitService.deleteUnit(unit_id);
        return ResultAPI.success();
    }
    @GetMapping("/showUnits")
    public ResultAPI showUnitTest(@RequestParam Integer semester_id){
        List<UnitDTO> units=unitService.findUnits(semester_id);
        return ResultAPI.success(units);
    }
    @GetMapping("/getUnitTests")
    public ResultAPI getUnitTests(@RequestParam Integer unit_id){
        log.info("获取单元测试问题列表！！！");
        List<StuUnitTest> tests=unitTestService.findTests(unit_id);
        for(StuUnitTest test:tests){
            //判断是否开始
            if(!CompareCurTime.compareTime(test.getStart_time())){
                //开始课程
                unitTestService.updateTestStatus(test.getUnit_test_id(),1);
                test.setStatus(1);
            }
            //判断是否过期
            if(!CompareCurTime.compareTime(test.getEnd_time())){
                //关闭课程
                unitTestService.updateTestStatus(test.getUnit_test_id(),0);
                test.setStatus(0);
            }
        }
        return ResultAPI.success(tests);
    }
    @GetMapping("/getTestQuestions")
    public ResultAPI getTest(@RequestParam Integer test_id){
        log.info("获取单元测试问题具体内容！！！");
        //求改paper有多少题目
        Integer qsNum=paperService.getQsCount(test_id);
        log.info("qsNum:{}",qsNum);
        //获得paper_id
        Integer paperId=paperService.getPaperId(test_id);
        if(qsNum==0){return ResultAPI.success();}
        //每一题的分数
        Double maxScore=100.0/qsNum;
        log.info("maxScore:{}",maxScore);
        paperService.updatePerMaxScore(paperId,maxScore);
        //每个选项
        TestPaper testPaper=new TestPaper();
        testPaper.setTest_id(test_id);
        testPaper.setPaper_id(paperId);
        List<Question> qsList=paperService.selectQSelectList(paperId);
        for(Question q:qsList){
            q.setMax_score(Math.round(maxScore * 100.0) / 100.0);
        }
        testPaper.setQuestionsList(qsList);
        return ResultAPI.success(testPaper);
    }
    @PostMapping("/submitTestAnswers")
    public ResultAPI submitTestAnswers(@RequestBody AnswerDTO answerDTO){
        //拿到最大分数
        log.info("完成测试并判题！！！！！");
        Double resScore=paperService.getresScore(answerDTO);
//        answerDTO.setRes_score(Math.round(resScore * 100.0) / 100.0);
        resScore=Math.round(resScore * 100.0) / 100.0;
        return ResultAPI.success(resScore);
    }


    @RequestMapping("findUnitBySemesterId")
    public ResultAPI findUnitBySemesterId(@RequestParam("semester_id") int semester_id){
        return unitService.findUnitBySemesterId(semester_id);
    }

    @RequestMapping("deleteUnitById")
    public ResultAPI deleteUnitById(@RequestParam("unit_id") int unit_id){
        return unitService.deleteUnitById(unit_id);
    }
    @PutMapping("updateUnitTitle")
    public ResultAPI updateUnit(@RequestBody UnitDTO unitDTO){
        return unitService.updateUnitTitle(unitDTO);
    }
}
